// EditSubclass.cpp: implementation of the CEditSubclass class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EditSubclass.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

COLORREF CEditSubclass::m_crFrmNor = RGB(100, 100, 100);
COLORREF CEditSubclass::m_crFrmFoc = RGB(0, 0, 255);
COLORREF CEditSubclass::m_crBackNor = RGB(240, 240, 255);
COLORREF CEditSubclass::m_crBackFoc = RGB(255, 255, 255);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CEditSubclass::CEditSubclass()
{

}

CEditSubclass::~CEditSubclass()
{

}

LRESULT CEditSubclass::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
{
	switch (msg){
		/*case WM_PAINT:
		case WM_ERASEBKGND:
			CWndSubclass::WindowProc(msg, wp, lp);
			OnPaint();
			return 0;*/
		case WM_NCPAINT:
			CWndSubclass::WindowProc(msg, wp, lp);
			OnNcPaint();
			return 0;
		case WM_SETFOCUS:
			CWndSubclass::WindowProc(msg, wp, lp);
			OnNcPaint();
			//OnPaint();
			return 0;
		case WM_KILLFOCUS:
			CWndSubclass::WindowProc(msg, wp, lp);
			OnNcPaint();
			return 0;
	}
	return CWndSubclass::WindowProc(msg, wp, lp);
}

void CEditSubclass::OnPaint()
{
	if (::GetFocus() == m_hWndHooked)
	{

	}
	else
	{
		CClientDC dc(m_hWndHooked);
		CRect rcClient;
		GetClientRect(m_hWndHooked, &rcClient);
		CBrush brush;
		brush.CreateSolidBrush(m_crBackNor);
		dc.FillRect(&rcClient, brush);
	
		//It is invalide for changing the bkcolor wiht the following code 
		/*CPaintDC dc(m_pWndHooked); 
	
		CRect rcClient;
		m_pWndHooked->GetClientRect(&rcClient);

		CDC memDC;
		memDC.CreateCompatibleDC(&dc);	
		CBitmap bitmap;
		bitmap.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());
		memDC.SelectObject(&bitmap);

		CWndSubclass::WindowProc(WM_PAINT, (WPARAM)memDC.m_hDC , 0);

		CDC maskDC;
		maskDC.CreateCompatibleDC(&dc);
		CBitmap maskBitmap;
		maskBitmap.CreateBitmap(rcClient.Width(), rcClient.Height(), 1, 1, NULL);
		maskDC.SelectObject(&maskBitmap);
		maskDC.BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &memDC, 
			rcClient.left, rcClient.top, SRCCOPY);
		
		CBrush brush(RGB(255, 255, 0));
		//brush.CreatePatternBrush(&m_bitmap);
		dc.FillRect(rcClient, &brush);	
		
		memDC.SetBkColor(RGB(0,0,0));          
		memDC.SetTextColor(RGB(255,255,255));  
		memDC.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), &maskDC, rcClient.left, rcClient.top, SRCAND);	
		
		dc.SetBkColor(RGB(255,255,255));
		dc.SetTextColor(RGB(0,0,0));
		dc.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), &maskDC, rcClient.left, rcClient.top, SRCAND);
		
		dc.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), &memDC, 
						rcClient.left, rcClient.top, SRCPAINT);
		brush.DeleteObject();*/
	}
}

void CEditSubclass::OnNcPaint()
{
	CWindowDC dcWin(m_hWndHooked);

	CRect rcWin;
	GetWindowRect(m_hWndHooked, &rcWin);
	rcWin -= rcWin.TopLeft();
	if (::GetFocus() == m_hWndHooked)
	{
		CRect rcLeftFrm(rcWin);
		rcLeftFrm.right = rcLeftFrm.left + 2;
		dcWin.Draw3dRect(&rcLeftFrm, RGB(160, 180, 200), RGB(100, 120, 140));

		CRect rcTopFrm(rcWin);
		rcTopFrm.bottom = rcTopFrm.top + 2;
		dcWin.Draw3dRect(&rcTopFrm, RGB(160, 180, 200), RGB(100, 120, 140));

		CRect rcRightFrm(rcWin);
		rcRightFrm.left = rcRightFrm.right - 2;
		dcWin.Draw3dRect(&rcRightFrm, RGB(100, 120, 140), RGB(160, 180, 200));

		CRect rcBottomFrm(rcWin);
		rcBottomFrm.top = rcBottomFrm.bottom - 2;
		dcWin.Draw3dRect(&rcBottomFrm, RGB(100, 120, 140), RGB(160, 180, 200));
	}
	else
	{
		/*CRect rcLeftFrm(rcWin);
		rcLeftFrm.right = rcLeftFrm.left + 2;
		dcWin.Draw3dRect(&rcLeftFrm, RGB(150, 150, 150), RGB(100, 100, 100));

		CRect rcTopFrm(rcWin);
		rcTopFrm.bottom = rcTopFrm.top + 2;
		dcWin.Draw3dRect(&rcTopFrm, RGB(150, 150, 150), RGB(100, 100, 100));

		CRect rcRightFrm(rcWin);
		rcRightFrm.left = rcRightFrm.right - 2;
		dcWin.Draw3dRect(&rcRightFrm, RGB(200, 200, 200), RGB(250, 250, 250));

		CRect rcBottomFrm(rcWin);
		rcBottomFrm.top = rcBottomFrm.bottom - 2;
		dcWin.Draw3dRect(&rcBottomFrm, RGB(200, 200, 200), RGB(250, 250, 250));*/
		CRect rcLeftFrm(rcWin);
		rcLeftFrm.right = rcLeftFrm.left + 2;
		dcWin.Draw3dRect(&rcLeftFrm, RGB(200, 200, 200), RGB(250, 250, 250));

		CRect rcTopFrm(rcWin);
		rcTopFrm.bottom = rcTopFrm.top + 2;
		dcWin.Draw3dRect(&rcTopFrm, RGB(200, 200, 200), RGB(250, 250, 250));

		CRect rcRightFrm(rcWin);
		rcRightFrm.left = rcRightFrm.right - 2;
		dcWin.Draw3dRect(&rcRightFrm, RGB(200, 200, 200), RGB(250, 250, 250));

		CRect rcBottomFrm(rcWin);
		rcBottomFrm.top = rcBottomFrm.bottom - 2;
		dcWin.Draw3dRect(&rcBottomFrm, RGB(200, 200, 200), RGB(250, 250, 250));
	}

}
